package com.foogui.primary;

import java.util.concurrent.*;

public class Establish {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //实现Runnable接口
        new Thread(()->{
            int sum=0;
            for (int i = 0; i < 10; i++) {
                sum+=i;
                System.out.println(Thread.currentThread().getName()+"-"+i);
            }
            System.out.println("自定义线程"+sum);
        }).start();

        //实现Callable接口
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<Integer> future = executorService.submit(() -> {
            int sum=0;
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"-"+i);
                sum+=i;
            }
            return sum;
        });
        System.out.println("Callable线程结果为："+future.get());

        //主线程
        int sum=0;
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"-"+i);
            sum+=i;
        }
        System.out.println("主线程"+sum);

        //FutureTask
        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
            return 111111;
        });
        executorService.submit(futureTask);
        System.out.println(futureTask.get());

    }
}
